package com.yyk.yiliao.ui.activity.detail.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import androidx.appcompat.app.AlertDialog;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.orhanobut.logger.Logger;
import com.umeng.socialize.UMShareAPI;
import com.yyk.yiliao.R;
import com.yyk.yiliao.base.BaseActivity;
import com.yyk.yiliao.aa.OnClickFlagUtil;
import com.yyk.yiliao.listener.DialogSureCancelListenter;
import com.yyk.yiliao.listener.ToolbarListenter;
import com.yyk.yiliao.ui.activity.dingdan.activity.CommodityOrde_Activity;
import com.yyk.yiliao.ui.gongyong.activity.PaySuccess_Activity;
import com.yyk.yiliao.ui.mine.activity.Registration_Activity;
import com.yyk.yiliao.util.DialogUtil;
import com.yyk.yiliao.util.Md5Util2;
import com.yyk.yiliao.util.TimerUtil;
import com.yyk.yiliao.util.ToastUtil;
import com.yyk.yiliao.util.glide.GlideUtils;
import com.yyk.yiliao.util.pay.PayUtils;
import com.yyk.yiliao.util.pay.PayUtils2;
import com.yyk.yiliao.util.rx.ApiService;
import com.yyk.yiliao.bean.OrderBusinessbankInfo;
import com.yyk.yiliao.bean.OrderCheckcodeInfo;
import com.yyk.yiliao.bean.OrderPaybalanceInfo;
import com.yyk.yiliao.bean.OrderPaysmsInfo;
import com.yyk.yiliao.util.rx.RxUtils;
import com.yyk.yiliao.bean.UserbankUsersInfo;
import com.yyk.yiliao.bean.UserbankMybankcardInfo;
import com.yyk.yiliao.bean.UserbankTestpwdInfo;
import com.yyk.yiliao.bean.WechatUnifiedorder_Info;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 支付订单页面
 */
public class PayPleaceOrder_Activity extends BaseActivity {
    public static PayPleaceOrder_Activity intanse;

    @BindView(R.id.bt_commit) //提交按钮
            Button btCommit;
    @BindView(R.id.tv_total) //金额
            TextView tvTotal;
    @BindView(R.id.checkBox) //微信
            CheckBox checkBox;
    @BindView(R.id.checkBox2)//支付宝
            CheckBox checkBox2;
    @BindView(R.id.rl_check)// 微信 点击
            RelativeLayout rlCheck;
    @BindView(R.id.rl_check2) //支付宝点击
            RelativeLayout rlCheck2;
    @BindView(R.id.checkBox3_yue) //余额支付
            CheckBox checkBox3Yue;
    @BindView(R.id.checkBox4_bankcard) //银行卡支付
            CheckBox checkBox4Bankcard;
    @BindView(R.id.tv_money) //余额
            TextView tvMoney;
    @BindView(R.id.rl_check3)
    RelativeLayout rlCheck3;
    @BindView(R.id.rl_check4)
    RelativeLayout rlCheck4;
    @BindView(R.id.rl_check4_view) //银行卡支付view
            RelativeLayout rlCheck4View;

    @BindView(R.id.ll_addbankcard) //其他银行卡
            LinearLayout llAddbankcard;
    @BindView(R.id.tv_addbankcard) //其他银行卡
            TextView tvAddbankcard;
    @BindView(R.id.iv_img) //银行卡图标
            ImageView ivImg;
    @BindView(R.id.tv_name) //银行卡银行名字
            TextView tvName;
    @BindView(R.id.tv_cardtype_number) //储蓄卡 卡号
            TextView tvCardtypeNumber;


    private String order_code; //订单号
    private float total; //价格
    private int endtype; //判断购物车结算还是重新下单 1.返回来的是购物车结算
    private float money;

    private int postion = 3; //默认余额支付 1.微信 2.支付宝3.余额4.银行卡
    private String cardId; //银行卡ID号
    private String banktel;//手机号
//    private boolean twosmsFlag;


    @Override
    public int getLayoutId() {
        return R.layout.activity_pay_pleace_order;
    }

    @Override
    public void initView() {
        initToolbar();
    }

    private void initToolbar() {
        setBackArrow(new ToolbarListenter() {
            @Override
            public void onClickListenter() {
                DialogUtil.getIntanse().showReturnTips(mActivity, "确定取消支付", "您的订单在30分钟内未支付将被取消\n请尽快完成支付", "确认离开", "继续支付", new DialogSureCancelListenter() {
                    @Override
                    public void onSureListenter() {

//                        if (ShopCart_Activity.intanse != null) {
//                            ShopCart_Activity.intanse.finish();
//                        }

                        Intent intent = new Intent();
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        if (endtype == 1) {
                            //商品购买
                            startAct(intent, CommodityOrde_Activity.class, null);
                        } else {
                            //预约挂号
                            startAct(intent, Registration_Activity.class, null);
                        }
                        finish();
                    }

                    @Override
                    public void onCancelListenter() {

                    }
                }, false);
            }
        });
        setTitle("收银台");
    }

    /**
     * 这个方法 可以改变返回键的状态,禁止返回弹出对话框
     * Android点击返回键back时弹出对话框Dialog - 开发者知识库  http://www.itdaan.com/blog/2014/06/22/6b173aebb9db.html
     *
     * @param keyCode
     * @param event
     * @return
     */

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            DialogUtil.getIntanse().showReturnTips(mActivity, "确定取消支付", "您的订单在30分钟内未支付将被取消\n请尽快完成支付", "确认离开", "继续支付", new DialogSureCancelListenter() {
                @Override
                public void onSureListenter() {

//                    if (ShopCart_Activity.intanse != null) {
//                        ShopCart_Activity.intanse.finish();
//                    }

                    Intent intent = new Intent();
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    if (endtype == 1) {
                        //商品购买
                        startAct(intent, CommodityOrde_Activity.class, null);
                    } else {
                        //预约挂号
                        startAct(intent, Registration_Activity.class, null);
                    }
                    finish();


                }

                @Override
                public void onCancelListenter() {
                }
            }, false);
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void initData() {
        initGetIntent();
        intanse = this;

    }

    private void initGetIntent() {
        if (getBundle() != null) {
            endtype = Integer.parseInt(getBundle().getString("endtype"));
            order_code = getBundle().getString("out_trade_no");
            Logger.e("商品购买订单号————" + order_code);

            total = Float.parseFloat(getBundle().getString("total"));
            tvTotal.setText(total + "");
            if (getBundle().containsKey("addtime")) {
                int addtime = Integer.parseInt(getBundle().getString("addtime")); //创建时间是固定的
                if (addtime != 0) {
                    timer = TimerUtil.get(addtime);
                    getTimeDuring(timer);
                }
            }
//            if (getBundle().containsKey("twosmsflag")) {
//                int addtime = Integer.parseInt(getBundle().getString("addtime")); //创建时间是固定的
//                boolean twosmsflag = getBundle().getBoolean("twosmsflag");
//                if (twosmsflag) {
//                    twosmsFlag = twosmsflag;
//                }
//
//            }

        }


    }

    @Override
    protected void onResume() {
        super.onResume();
        //患者 余额
        initPostUserbankUsers();
        //我的银行卡列表
        initUserbankMybankcardt();

    }

    //银行卡列表
    private void initUserbankMybankcardt() {
        Map<String, String> map = new TreeMap<>();
        map.put("id", Hawk.get("uid") + "");
        map.put("sign", Md5Util2.createSign(map));
        RxUtils.getInstance().retrofit.create(ApiService.class).postUserbankMybankcard(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<UserbankMybankcardInfo>() {


                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Logger.e("我的银行 卡列表" + e.getLocalizedMessage());

                    }

                    @Override
                    public void onNext(UserbankMybankcardInfo info) {
                        Logger.e("我的银行 卡列表" + info.toString());
                        if (info.getCode() == 1) {
                            rlCheck4View.setVisibility(View.VISIBLE);
                            tvAddbankcard.setText("其他银行卡");
                            List<UserbankMybankcardInfo.DataBean> data = info.getData();
                            UserbankMybankcardInfo.DataBean dataBean = data.get(0);
                            if (dataBean != null) {
                                GlideUtils.loadImage(mActivity, ApiService.BASE_URL + dataBean.getImg(), ivImg);
                                tvCardtypeNumber.setText(dataBean.getBank_name() + " (尾数" + dataBean.getNumberu() + ")");
                                tvName.setText(dataBean.getBank_name());
                                cardId = String.valueOf(dataBean.getBindid());
                                banktel = "验证码已发送手机号" + dataBean.getBanktel();
                            }
                        } else {
                            rlCheck4View.setVisibility(View.GONE);
                            tvAddbankcard.setText("请添加银行卡");

                        }
                    }
                });


    }

    //患者 余额
    private void initPostUserbankUsers() {


        Map<String, String> map = new TreeMap<>();
        map.put("id", Hawk.get("uid") + "");
        map.put("sign", Md5Util2.createSign(map));
        RxUtils.getInstance().retrofit.create(ApiService.class).postUserbankUsers(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Subscriber<UserbankUsersInfo>() {
                    @Override
                    public void onCompleted() {
                        //完成的时候调用
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(UserbankUsersInfo info) {
                        Logger.e("患者主页" + info.toString());
                        money = info.getData().getMoney();
                        tvMoney.setText("可用余额" + money + "元");
                    }
                });
    }


    private void clearCheckBox() {
        checkBox.setChecked(false);
        checkBox2.setChecked(false);
        checkBox3Yue.setChecked(false);
        checkBox4Bankcard.setChecked(false);
    }

    /**
     * 1. 微信
     * 2
     * 3.支付宝
     */

    @OnClick({R.id.rl_check, R.id.rl_check2, R.id.rl_check3, R.id.rl_check4, R.id.ll_addbankcard, R.id.checkBox, R.id.checkBox2, R.id.checkBox3_yue, R.id.checkBox4_bankcard, R.id.bt_commit})
    public void onViewClicked(View view) {
        if (OnClickFlagUtil.isFastClick()) {
            return;
        }
        switch (view.getId()) {

            case R.id.rl_check:
                clearCheckBox();
                checkBox.setChecked(true);
                postion = 1;
                break;
            case R.id.rl_check2:
                clearCheckBox();
                checkBox2.setChecked(true);
                postion = 2;
                break;

            case R.id.rl_check3:
                clearCheckBox();
                checkBox3Yue.setChecked(true);
                postion = 3;
                break;
            case R.id.rl_check4:
                clearCheckBox();
                checkBox4Bankcard.setChecked(true);
                postion = 4;
                break;
            case R.id.ll_addbankcard:
                DialogUtil.getIntanse().showPayFangshiDialog(mActivity, true, true, new DialogUtil.SelectPayListenter() {
                    @Override
                    public void onClick(String img, String name, String id, String phone, AlertDialog dialog) {
                        GlideUtils.loadImage(mActivity, ApiService.BASE_URL + img, ivImg);
                        tvCardtypeNumber.setText(name);
                        banktel = phone;
                        cardId = id;
                        dialog.dismiss();
                    }
                });
                break;


            case R.id.bt_commit:
                if (postion == 1) {
                    WeixinPay();
                } else if (postion == 2) {
                    PayUtils ZFBpay = new PayUtils(mActivity);
                    ZFBpay.setAlipayBack(new PayUtils.AlipayBack() {
                        @Override
                        public void payBack(boolean status) {
                            if (status) { //true为支付成功 false失败
                                goToNext();
                            }
                        }
                    });
                    ZFBpay.payV2(total, order_code);
                } else if (postion == 3) {
                    DialogUtil.getIntanse().showPayDialog(mActivity, new DialogUtil.PayListenter() {
                        @Override
                        public void onClick(String password, AlertDialog dialog, TextView textView) {
                            if (password.length() == 6) {
                                Map<String, String> map = new TreeMap<>();
                                map.put("id", Hawk.get("uid") + "");
                                map.put("pwd", password + "");
                                map.put("sign", Md5Util2.createSign(map));
                                RxUtils.getInstance().retrofit.create(ApiService.class).postUserbankTestpwd(map)
                                        .subscribeOn(Schedulers.io())     //调度线程
                                        .observeOn(AndroidSchedulers.mainThread())
                                        .unsubscribeOn(Schedulers.io())   //取消订阅
                                        .subscribe(new Subscriber<UserbankTestpwdInfo>() {
                                            @Override
                                            public void onNext(UserbankTestpwdInfo info) {
                                                Logger.e("检验交易密码" + info.toString());
                                                if (info.getCode() == 1) {
                                                    Map<String, String> map = new TreeMap<>();
                                                    map.put("order_code", order_code);
                                                    map.put("type", endtype + ""); //1商品购买2预约挂号
                                                    map.put("sign", Md5Util2.createSign(map));
                                                    RxUtils.getInstance().retrofit.create(ApiService.class).postOrderPaybalance(map)
                                                            .subscribeOn(Schedulers.io())     //调度线程
                                                            .observeOn(AndroidSchedulers.mainThread())
                                                            .unsubscribeOn(Schedulers.io())   //取消订阅
                                                            .subscribe(new Subscriber<OrderPaybalanceInfo>() {
                                                                @Override
                                                                public void onCompleted() {
                                                                    //完成的时候调用
                                                                }

                                                                @Override
                                                                public void onError(Throwable e) {
                                                                    e.printStackTrace();
                                                                }

                                                                @Override
                                                                public void onNext(OrderPaybalanceInfo info) {
                                                                    Logger.e("余额支付" + info.toString());
                                                                    if (info.getCode() == 1) {
                                                                        goToNext();
                                                                        dialog.dismiss();
                                                                        return;
                                                                    } else if (info.getCode() == 2) {
                                                                        ToastUtil.showShort(mActivity, "余额不足");
                                                                        dialog.dismiss();
                                                                        return;
                                                                    } else if (info.getCode() == 3) {
                                                                        ToastUtil.showShort(mActivity, "余额支付失败");
                                                                    } else if (info.getCode() == 4) {
                                                                        ToastUtil.showShort(mActivity, "未找到待支付订单");
                                                                    } else {
                                                                        ToastUtil.showShort(mActivity, "支付失败");
                                                                    }

                                                                }
                                                            });
                                                } else if (info.getCode() == 2) {
                                                    textView.setVisibility(View.VISIBLE);
                                                } else {
                                                    textView.setVisibility(View.VISIBLE);
                                                    ToastUtil.showShort(mActivity, info.getMsg());
                                                }
                                            }

                                            @Override
                                            public void onCompleted() {
                                            }

                                            @Override
                                            public void onError(Throwable e) {

                                            }
                                        });
                            } else {
                                textView.setVisibility(View.GONE);
                            }
                        }
                    });

                } else if (postion == 4) {
                    Map<String, String> map = new TreeMap<>();
                    map.put("cardid", cardId);
                    map.put("order_code", order_code);
                    map.put("goodsDesc", "商品购买");
                    map.put("sign", Md5Util2.createSign(map));
                    RxUtils.getInstance().retrofit.create(ApiService.class).postOrderBusinessbank(map)
                            .subscribeOn(Schedulers.io())
                            .observeOn(AndroidSchedulers.mainThread())
                            .unsubscribeOn(Schedulers.io())
                            .subscribe(new Subscriber<OrderBusinessbankInfo>() {
                                @Override
                                public void onCompleted() {

                                }

                                @Override
                                public void onError(Throwable e) {

                                }

                                @Override
                                public void onNext(OrderBusinessbankInfo info) {
                                    Logger.e("充值 银行" + info.toString());
                                    if (info.getCode() == 1) {

                                        String order_codes = info.getData().getOrder_code();
                                        String tokens = info.getData().getToken();
                                        DialogUtil.getIntanse().showPhoneSmsDialog(mActivity, new DialogUtil.RechargeListenter() {
                                            @Override
                                            public void onClick(String password, AlertDialog dialog) {

                                                Map<String, String> map = new TreeMap<>();
                                                map.put("token", tokens);
                                                map.put("code", password + "");
                                                map.put("order_code", order_codes);
                                                map.put("sign", Md5Util2.createSign(map));
                                                RxUtils.getInstance().retrofit.create(ApiService.class).postOrderCheckcode(map)
                                                        .subscribeOn(Schedulers.io())
                                                        .observeOn(AndroidSchedulers.mainThread())
                                                        .unsubscribeOn(Schedulers.io())
                                                        .subscribe(new Subscriber<OrderCheckcodeInfo>() {
                                                            @Override
                                                            public void onCompleted() {

                                                            }

                                                            @Override
                                                            public void onError(Throwable e) {
                                                                Logger.e("接口22：验证支付短信（ggk" + e.getMessage());

                                                            }

                                                            @Override
                                                            public void onNext(OrderCheckcodeInfo info) {
                                                                Logger.e("接口22：验证支付短信（ggk" + info.toString());
                                                                if (info.getCode() == 1) {
                                                                    goToNext();
                                                                    dialog.dismiss();
                                                                } else {
                                                                    ToastUtil.showShort(mActivity, info.getMsg());
                                                                }
                                                            }
                                                        });

                                            }

                                            @Override
                                            public void onTextPhone(TextView textView, TextView
                                                    textView2) {
                                                textView.setText(banktel);
                                                timeCount = new TimeCount(60000, 1000, textView2);
                                                timeCount.start();
                                            }

                                            @Override
                                            public void postHttp() {

                                            }

                                            @Override
                                            public void sendSms(TextView sms) {
                                                Map<String, String> map = new TreeMap<>();
                                                map.put("token", tokens);
                                                map.put("sign", Md5Util2.createSign(map));
                                                RxUtils.getInstance().retrofit.create(ApiService.class).postOrderPaysms(map)
                                                        .subscribeOn(Schedulers.io())
                                                        .observeOn(AndroidSchedulers.mainThread())
                                                        .unsubscribeOn(Schedulers.io())
                                                        .subscribe(new Subscriber<OrderPaysmsInfo>() {
                                                            @Override
                                                            public void onCompleted() {

                                                            }

                                                            @Override
                                                            public void onError(Throwable e) {

                                                            }

                                                            @Override
                                                            public void onNext(OrderPaysmsInfo info) {
                                                                Logger.e("接口23：重发支付短信222222" + info.toString());
                                                                if (info.getCode() == 1) {
                                                                    ToastUtil.showShort(mActivity, "获取验证码");
                                                                    timeCount = new TimeCount(60000, 1000, sms);
                                                                    timeCount.start();
                                                                } else {
                                                                    ToastUtil.showShort(mActivity, info.getMsg());
                                                                }
                                                            }
                                                        });
                                            }

                                        });

                                    } else if (info.getCode() == 99) {

                                    } else {
                                        ToastUtil.showShort(mActivity, info.getMsg());
                                    }
                                }
                            });


                }
                break;
        }
    }

    /*************************************************************************************************/

    private void WeixinPay() {
        Map<String, String> map = new TreeMap<>();
        map.put("order_code", order_code);
        map.put("sub_type", endtype + ""); //1. 商品购买   2.预约挂号
        map.put("type", 1 + ""); //设备来源 android
        map.put("sign", Md5Util2.createSign(map));
        RxUtils.getInstance().retrofit.create(ApiService.class).postWechatUnifiedorder(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Subscriber<WechatUnifiedorder_Info>() {
                    @Override
                    public void onCompleted() {
                        //完成的时候调用
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(WechatUnifiedorder_Info info) {
                        Logger.e("  微信返回值" + info.toString());
                        if (info.getCode() == 1) {
                            PayUtils2 instance = PayUtils2.getInstance(mActivity);
                            instance.invokeWxPay(info);
                            instance.setWxBack(new PayUtils2.WXBack() {
                                @Override
                                public void payBack(boolean status) {
                                    if (status) {
                                        goToNext();
                                    }
                                }
                            });
                        }
                    }
                });
    }

    /**
     * 默认为活动支付
     * endtype=1 商品购物
     * endtype=2 预约挂号
     * postion = 1 为 微信 购物车结算
     * postion = 2 为 支付宝购物车结算
     * postion = 3 为 余额购物车结算
     * postion = 4 为 银行卡购物车结算
     * 其他是活动支付
     */
    private void goToNext() {
        Bundle bundle;
        if (endtype == 1) {
            if (postion == 1 || postion == 2) {
                bundle = new Bundle();
                bundle.putString("money", total + "");
                bundle.putString("flag", endtype + "");
                if (postion == 1)
                    bundle.putString("paymonth", "微信支付");
                else if (postion == 2)
                    bundle.putString("paymonth", "支付宝支付");

                startAct(PaySuccess_Activity.class, bundle); //支付成功
            } else if (postion == 3 || postion == 4) {
                bundle = new Bundle();
                bundle.putString("money", tvTotal.getText().toString().trim());
                bundle.putString("flag", endtype + "");
                if (postion == 3)
                    bundle.putString("paymonth", "余额支付");
                else if (postion == 4)
                    bundle.putString("paymonth", "银行卡支付");
                startAct(PaySuccess_Activity.class, true, bundle);
            }

        } else if (endtype == 2) {
            if (postion == 1 || postion == 2) {
                bundle = new Bundle();
                bundle.putString("money", total + "");
                bundle.putString("flag", endtype + "");
                if (postion == 1)
                    bundle.putString("paymonth", "微信支付");
                else if (postion == 2)
                    bundle.putString("paymonth", "支付宝支付");

                startAct(PaySuccess_Activity.class, bundle); //支付成功
            } else if (postion == 3 || postion == 4) {
                bundle = new Bundle();
                bundle.putString("money", tvTotal.getText().toString().trim());
                bundle.putString("flag", endtype + "");
                if (postion == 3)
                    bundle.putString("paymonth", "余额支付");
                else if (postion == 4)
                    bundle.putString("paymonth", "银行卡支付");
                startAct(PaySuccess_Activity.class, true, bundle);
            }

        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

/*************************************************************************************************/

    /**
     * https://blog.csdn.net/u010575303/article/details/54692567  支付倒计时
     */
    private String timer;
    @BindView(R.id.tv_addtime)
    TextView tvAddtime;//倒计时
    private long countdownTime;//倒计时的总时间(单位:毫秒)
    private String timefromServer;//从服务器获取的订单生成时间
    private long chaoshitime;//从服务器获取订单有效时长(单位:毫秒)

    Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            countdownTime -= 1000;//倒计时总时间减1
            SimpleDateFormat minforamt = new SimpleDateFormat("mm:ss");
            String hms = minforamt.format(countdownTime);//格式化倒计时的总时间
            Logger.e("支付剩余时间:" + hms);
//            Logger.e("countdownTime:" + countdownTime);
//            Logger.e("duringTime" + countdownTime);
            if (countdownTime < 0) {
                Logger.e("支付剩余时间: 00:00");
                DialogUtil.getIntanse().showReturnTips(mActivity, "", "订单时间超时", "确定", "", new DialogSureCancelListenter() {
                    @Override
                    public void onSureListenter() {
                        finish();
                    }

                    @Override
                    public void onCancelListenter() {

                    }
                }, true);
                handler.removeCallbacks(runnable);
                return;
            } else {
                tvAddtime.setText("支付剩余时间：" + hms);
            }
            handler.postDelayed(this, 1000);
        }
    };


    private void getTimeDuring(String timefromServer) {
        chaoshitime = 30 * 60 * 1000;
        //        timefromServer = "2017-01-23 11:40:50";//应该从服务器获取
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date serverDate = df.parse(timefromServer);
            long duringTime = new Date().getTime() - serverDate.getTime();//计算当前时间和从服务器获取的订单生成时间的时间差
            countdownTime = chaoshitime - duringTime;//计算倒计时的总时间

            handler.postDelayed(runnable, 1000);
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }
/*************************************************************************************************/

    /**
     * 发送验证码
     */
    private TimeCount timeCount;


    /**
     * 设置 60秒时间 倒计时
     */
    private class TimeCount extends CountDownTimer {
        private TextView tvSendsms;

        public TimeCount(long millisInFuture, long countDownInterval, TextView tvSendsms) {
            super(millisInFuture, countDownInterval);
            this.tvSendsms = tvSendsms;
        }

        @Override
        public void onTick(long millisUntilFinished) {
//            mLoginEdit.setBackgroundColor(Color.parseColor("#B6B6D8"));
            tvSendsms.setClickable(false);
            tvSendsms.setText(millisUntilFinished / 1000 + "s后重发");
            tvSendsms.setTextColor(getResources().getColor(R.color.gray_c1));

        }

        @Override
        public void onFinish() {
            tvSendsms.setClickable(true);
            tvSendsms.setText("获取验证码");
            tvSendsms.setTextColor(getResources().getColor(R.color.gray_c1));

        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timeCount != null) {
            timeCount.cancel();
        }
        handler.removeCallbacks(runnable);
    }
    /*************************************************************************************************/

}
